﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OnTheWay.Functions;

namespace PcBackgroundMgr.Controls
{
    /// <summary>
    /// Pagination.xaml 的交互逻辑
    /// </summary>
    public partial class Pagination : UserControl
    {
        public Pagination()
        {
            InitializeComponent();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            FunctionCtrlListBase ctrl = DataContext as FunctionCtrlListBase;
            if (ctrl != null)
            {
                if (ctrl.PageIndex > 1)
                {
                    ctrl.PageIndex = 1;
                    ctrl.CommandRefresh.Execute(sender);
                    btnNext.IsEnabled = true;
                    btnLast.IsEnabled = true;
                }
                btnFirst.IsEnabled = false;
                btnPrev.IsEnabled = false;
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            FunctionCtrlListBase ctrl = DataContext as FunctionCtrlListBase;
            if (ctrl != null)
            {
                if (ctrl.PageIndex > 1)
                {
                    ctrl.PageIndex -= 1;
                    ctrl.CommandRefresh.Execute(sender);
                    btnNext.IsEnabled = true;
                    btnLast.IsEnabled = true;
                }
                if (ctrl.PageIndex == 1)
                {
                    btnFirst.IsEnabled = false;
                    btnPrev.IsEnabled = false;
                }
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            FunctionCtrlListBase ctrl = DataContext as FunctionCtrlListBase;
            if (ctrl != null)
            {
                if (ctrl.PageIndex < ctrl.PageCount)
                {
                    ctrl.PageIndex += 1;
                    ctrl.CommandRefresh.Execute(sender);
                    btnFirst.IsEnabled = true;
                    btnPrev.IsEnabled = true;
                }
                if (ctrl.PageIndex == ctrl.PageCount)
                {
                    btnNext.IsEnabled = false;
                    btnLast.IsEnabled = false;
                }
            }
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            FunctionCtrlListBase ctrl = DataContext as FunctionCtrlListBase;
            if (ctrl != null)
            {
                if (ctrl.PageIndex < ctrl.PageCount)
                {
                    ctrl.PageIndex = (int)ctrl.PageCount;
                    ctrl.CommandRefresh.Execute(sender);
                    btnFirst.IsEnabled = true;
                    btnPrev.IsEnabled = true;
                }
                btnNext.IsEnabled = false;
                btnLast.IsEnabled = false;
            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            FunctionCtrlListBase ctrl = DataContext as FunctionCtrlListBase;
            if (ctrl != null)
            {
                ctrl.CommandRefresh.Execute(sender);
            }
        }

        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(int), typeof(Pagination),
            new FrameworkPropertyMetadata(0, OnPageSizePropertyChanged));

        public static void OnPageSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Pagination ctrl = d as Pagination;
            ctrl.RefreshButton();
        }


        public int TotalCount
        {
            get { return (int)GetValue(TotalCountProperty); }
            set { SetValue(TotalCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TotalCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register("TotalCount", typeof(int),typeof(Pagination),
            new FrameworkPropertyMetadata(0, OnTotalCountPropertyChanged));

        public static void OnTotalCountPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Pagination ctrl = d as Pagination;
            ctrl.RefreshButton();
        }

        private void RefreshButton()
        {
            FunctionCtrlListBase ctrl = DataContext as FunctionCtrlListBase;
            if (ctrl != null)
            {
                btnFirst.IsEnabled = ctrl.PageIndex > 1;
                btnPrev.IsEnabled = ctrl.PageIndex > 1;
                btnNext.IsEnabled = ctrl.PageIndex < ctrl.PageCount;
                btnLast.IsEnabled = ctrl.PageIndex < ctrl.PageCount;
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            SetBinding(PageSizeProperty, new Binding("PageSize"));
            SetBinding(TotalCountProperty, new Binding("PageCount"));
        }

    }
}
